//
//  CZStatusViewModels.swift
//  WeiBO28
//
//  Created by 飞奔的羊 on 16/5/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZStatusViewModels: NSObject {
	var status: CZStatus?
	
	//转发微博内容
	var retweetContent: String?
	
	//提供转发数内容
	var retweetCount: String?
	
	var commentConunt: String?
//	赞
	var unlikeCount: String?
	
	//来源
	var sourceContent: String?
	//会员等级图片
	var mbrankImage: UIImage?
	
	//认证等级类型图片
	var verfiedTypeImage: UIImage?
	//时间,定义计算属性的目的
	var timeContent: String? {
		get {
		//获取发微博时间
			guard let creatAt = status?.created_at else {
			
				return nil
			}
//sinaDateString里面的 self 就是当前调用的对象
			return NSDate.sinaDate(creatAt).sinaDateString

		}
	
	}
	
	//原创微博副文本
	var orgianlAtributedString: NSAttributedString?
	var retweetAttributedString: NSAttributedString?
	
	init(status: CZStatus) {
		super.init()
		self.status = status
		hanleRetWeetContentView(status)
		retweetCount = handleCountContent(status.reposts_count, defaultTitle: "转发")
		//评论数
		commentConunt = handleCountContent(status.comments_count, defaultTitle: "评论")
		unlikeCount = handleCountContent(status.attitudes_count, defaultTitle: "赞")
		
		handlMbrankImage(status.user?.mbrank ?? 0)
		//处理来源数据
		handleSourceContent(status)
		//处理会员认证等级类型图片显示
		hanldeVerFiedTypeImage(status.user?.verified_type ?? 0)
	//处理微博内容的表情字符串
		orgianlAtributedString = handleEmoticonContentWithStatus(status.text!)
	}
	//通过微博内容,处理里面的表情字符串转表情副文本
	private func handleEmoticonContentWithStatus(status: String) -> NSAttributedString {
		//将微博内容转成副文本,便于后面使用,直接设置给我们原创微博与转发微博的副文本
	 let result = NSMutableAttributedString(string: status)
		// 根据微博内容匹配表情字符串,正则表达式
		var matchResultArray = [CZMatchResyukt]()
	
		(status as NSString).enumerateStringsMatchedByRegex("\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]") { (captureCount, captureString, captureRange, _) in
			if let emoticonStr = captureString.memory as? String {
			let matchResult = CZMatchResyukt(string: emoticonStr, range: captureRange.memory)
				matchResultArray.append(matchResult)
			}
		}
		for value in matchResultArray.reverse()
		{
			if let emoticon = CZEmotionTools.sharedTools.emoticonWithEMoticonStr(value.string){
    //创建表情副文本
				let emoticonAttr = NSAttributedString.attributedWithEmoticon(emoticon, font: UIFont.systemFontOfSize(HomeFontSize))
				//替换表情副文本
				result.replaceCharactersInRange(value.range, withAttributedString: emoticonAttr)
			}
		}
		
		return result
	}
	
	//换发微博内容,拼接字符串
	private func hanleRetWeetContentView(status: CZStatus) {
	
		guard let retweet = status.retweeted_status?.text else {
		
		return
		}
	//代码执行到这,一定存在
		guard let name  = status.retweeted_status?.user?.screen_name else {
		
		return
		}
		//拼接转发微博内容
		retweetContent = "@\(name): \(retweet)"
		
	}
	//处理转发评论赞
	private func handleCountContent(count: Int, defaultTitle: String) -> String {
		if count > 0 {
		
			if count >= 10000{
			
				let result = CGFloat(count / 1000 / 10)
				let resultStr = "\(result)万"
				if resultStr.containsString(".0") {
					return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
				}
			} else {
			return "\(count)"
			}
		
		} else {
		
			return defaultTitle
		}
		
		return defaultTitle
		
	}
	
	//处理来源数据
	private func handleSourceContent(status: CZStatus) {
	
		guard let source = status.source else {
		
		return
		}
		//  \ 是转移符号
		if source.containsString("\">") {
			
			 let startIndex = source.rangeOfString("\">")?.endIndex
			let endIndex = source.rangeOfString("</")?.startIndex
			
			if let start = startIndex, end = endIndex {
    sourceContent = source.substringWithRange(start..<end)
			}
		}
	
	}
	//处理认证类型等级图片
	private func hanldeVerFiedTypeImage(verfied: Int) {
	//认证类型 -1
		switch verfied {
		case 0:
			verfiedTypeImage = UIImage(named: "avatar_vip")
		case 2,3,5:
			verfiedTypeImage = UIImage(named: "avatar_enterprise_vip")
			case 220:
			verfiedTypeImage = UIImage(named: "avatar_grassroot")
		
		default:
			verfiedTypeImage = nil
		}
	
	}
	//处理会员等级图片
	
	private func isThisYear(creatAtDate: NSDate) -> Bool {
	//获取当前时间
		let currentDate = NSDate()
	
		let dateFormatter = NSDateFormatter()
		//设置本地化信息
		dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
		//设置格式化方式
		dateFormatter.dateFromString("yyyy")
		//获取当前时间的年份
		let currentDateYear = dateFormatter.stringFromDate(currentDate)
		//获取发微博时间的年份
		let createAtDateYear = dateFormatter.stringFromDate(creatAtDate)
		
		return currentDateYear == createAtDateYear
		
		
		
	
	}
	
	private func handlMbrankImage(count: Int) {
	
		if count > 0 && count < 7 {
			mbrankImage = UIImage(named: "common_icon_membership_level\(count)")
//			print("-----\(count)-----")
		}
	}
	
}










